<?php
/**
 * Created By Project
 * Author: 陈东
 * Copyright：陈东
 * Source Code：陈东
 * Explain：I shall not authorize for any other commercial purposes, law breakers must be prosecuted.
 */
namespace Liveadmin\Controller;
use Think\Controller;

class IndexController extends BaseController {

    /*
     * 直播列表显示
     */
    public function main(){
    	$type = D(C('TABLE_LIVEING'));
    	session('userinfo')['type'] ? $where = null : $where['live_userid'] = session('userinfo')['id'];
    	$count = $type->where($where)->count();
    	//每页显示条数
    	$num = C('MAIN_NUM');
    	$page = new \Think\Page($count,$num);
    	$obj = pagestyle($page);
    	$show = $obj->show();
    	$result = $type->order('live_push desc,id desc')->where($where)->limit($obj->firstRow.','.$obj->listRows)->select();

    	$this->assign('list',$result);
    	$this->assign('page',$show);
    	$this->display('main');
    }

	/*添加直播*/
	public function live(){
		$this->liveinfo();
		$this->display();
	}

	/*修改*/
	public function edit(){
		$id = I('get.id');
		$this->liveinfo($id);
		$this->display('live');
	}

	/*
	 * 添加或修改
	 */
	public function livehandle(){
		$liveway = I('post.live_way');
		if(empty($liveway)){
			$this->error('观看方式错误','',1);
		}
		/*验证规则*/
		$rules = array(
			array('live_domain','require','domainid不能为空！'),
			array('live_name','require','直播标题不能为空！'),
			array('live_img','require','直播图片不能为空！'),
			array('live_content','require','直播简介不能为空！'),
		);
		$liveobj = D(C('TABLE_LIVEING'));
		if(!$liveobj->validate($rules)->create()){
			$this->error($liveobj->getError(),'',1);
			exit;
		}
		$handlytype = I('post.handlytype');
		/*数据插入或更新*/
		//添加
		if(empty($handlytype)){
			$liveobj->live_releasetime = date("Y-m-d H:i:m");
			$liveobj->live_userid = session('userinfo')['id'];
			$liveobj->live_content = html_entity_decode(I('post.live_content'));
			$res = $liveobj->add();
			if($res){
				$cache = A('Cache');
				$cache->cacheLivelist();
				$cache->findlive($res);
				$this->success('添加成功','/Liveadmin/Index/main');
			}else{
				$this->error('添加失败','',1);
			}
		//修改
		}else{
			$live_id = I('post.live_id');
			if($liveway==2){
				$liveobj->live_pre = null;
				$this->changerecord($live_id,'consume_moneynum');
			}else if($liveway==1){
				$liveobj->live_dirll = null;
				$this->changerecord($live_id,'consume_dirllnum');
			}
			$liveobj->live_content = html_entity_decode(I('post.live_content'));
			$res = $liveobj->where(array('id'=>$live_id))->save();
			if($res>=0){
				$cache = A('Cache');
				$cache->cacheLivelist();
				$cache->findlive($live_id);
				$this->success('修改成功','/Liveadmin/Index/main');
			}else{
				$this->error('更新失败','',1);
			}
		}
	}

	//修改已支付的现金或钻观看的记录
	protected function changerecord($liveid,$field){
		$userinfo = $_COOKIE['user_userinfo'];
		$mconsume = D(C('TABLE_MONEYCONSUME'));
		$data[$field] = null;
		$res = $mconsume->where(array('live_id'=>$liveid,'user_token'=>$userinfo))->save($data);
		return $res;
	}

	/*某条直播信息*/
	public function liveinfo($liveid=null){
		if(!empty($liveid)){
			$liveobj = D(C('TABLE_LIVEING'));
			$res = $liveobj->find($liveid);
			$this->assign('liveinfo',$res);
			$this->assign('edit',1);
		}else{
			$this->assign('liveinfo',null);
			$this->assign('add',1);
		}
	}

	/*
	 * 删除直播
	 * 逻辑删除 Live_status 设为-1
	 */
	public function delete(){
		$id = I('get.id');
		$s = I('get.del');
		if(!empty($id)){
			$liveobj = D(C('TABLE_LIVEING'));
			if($s==1){
				$data['live_status'] = -1;
			}else{
				$data['live_status'] = null;
			}
			$res = $liveobj->where(array('id'=>$id))->save($data);
			if($res){
				echo '<script>location.href="/Liveadmin/Index/main";</script>';
				//$this->success('执行成功','/Live/Index/main');
			}
		}else{
			echo "<script>alert('执行失败！');location.href='/Liveadmin/Index/main';</script>";
			//$this->error('执行失败','',1);
		}
	}

	public function livestatistics(){
		$type = D(C('TABLE_LIVEING'));
		$res = $type->select();
		$this->assign('livelist',$res);
		$this->display();
	}
	/*
	 * 显示图表
	 */
	public function statistics(){
		$liveid = I('get.liveid');
		if($liveid<1){
			$liveid = null;
		}
		//echo $this->showdatainfo('pv_num',$liveid);die;
		$pv_num = $this->showdatainfo('pv_num',$liveid);
		$ip_num = $this->showdatainfo('ip_num',$liveid);
		$uv_num = $this->showdatainfo('uv_num',$liveid);
		$this->assign('max',$this->getVisitedMax($pv_num,$ip_num,$uv_num));
		$this->assign('pv_num',$pv_num);
		$this->assign('ip_num',$ip_num);
		$this->assign('uv_num',$uv_num);
		$d = null;
		for($i=1;$i<=$this->getdnum();$i++){
			$d .= $i.",";
		}
		$this->assign('d',rtrim($d,','));
		$this->display();
	}
	//根据liveid显示访问量信息
	protected function showdatainfo($field,$liveid=null){
		$tjnum = D(C('TABLE_TJNUM'));
		if(!empty($liveid)){
			$where['live_id'] = $liveid;
		}else{
			$where = null;
		}
		$res = $tjnum->where($where)->select();
		if($res){
			$num = null;
			$date1 = date('Y')."-".date('m')."-";
			for($i=1;$i<=$this->getdnum();$i++){
				if(strlen($i)<2) $i = '0'.$i;
				$date2 = null;
				$date2 = $date1.$i;
				if(!empty($liveid)){
					foreach($res as $v){
						if($date2==date('Y-m-d',strtotime($v['visitdate']))){
							$n = $v[$field];
						}else{
							$n = 0;
						}
						$num .= $n.',';
					}
				}else{
					$n1 = 0;
					foreach($res as $v){
						if($date2==date('Y-m-d',strtotime($v['visitdate']))){
							$n1 += $v[$field];
						}else{
							$n1 += 0;
						}
					}
					$num .= $n1.',';
				}

			}
			$num1 = rtrim($num,',');
			return $num1;
		}else{
			$num = null;
			for($i=1;$i<=$this->getdnum();$i++){
				$num .= '0,';
			}
			$num1 = rtrim($num,',');
			return $num1;
		}
	}
	//获取天数
	protected function getdnum(){
		return $day = date('t');
	}
	//获取访问量最大的数
	protected function getVisitedMax($pv_num,$ip_num,$uv_num){
		$pv_num = explode(',',$pv_num);
		$ip_num = explode(',',$ip_num);
		$uv_num = explode(',',$uv_num);
		rsort($pv_num);
		rsort($ip_num);
		rsort($uv_num);
		$max = array();
		$max[] = $pv_num[0];
		$max[] = $ip_num[0];
		$max[] = $uv_num[0];
		rsort($max);
		return floor($max[0]*1.5);
	}

	/*
	 * 省份比例
	 */
	public function statisticsarea(){
		$liveid = I('get.liveid');
		$tj = D(C('TABLE_TJ'));
		if(!empty($liveid)){
			$where = "live_id=$liveid and region!=''";
		}else{
			$where = "region!=''";
		}
		$num = $tj->where($where)->count();
		$res = $tj->group('region')->where($where)->select();
		foreach($res as $key=>&$v){
			$v['scale'] = $this->getregioncount($liveid,$v['region'])/$num*100;
			$v['bg1'] = imcolor()[0][$key];
			$v['bg2'] = imcolor()[1][$key];
		}
		$res = arr_sort($res,'scale','desc');
		$this->assign('regionlist',$res);
		$this->display();
	}
	//根据省份获取省份条数
	public function getregioncount($liveid=null,$regionname){
		$tj = D(C('TABLE_TJ'));
		if(!empty($liveid)){
			$where = "live_id=$liveid and region='".$regionname."'";
		}else{
			$where = "region='".$regionname."'";
		}
		$num = $tj->where($where)->count();
		return $num;
	}



}